//package com.up.vms.infrastructure.mvc.aop;
//
//import com.up.vms.infrastructure.mvc.BaseRequest;
//import com.up.vms.infrastructure.mvc.dto.EdiHeaders;
//import com.up.vms.infrastructure.mvc.dto.EdiHttpHeader;
//import com.up.vms.infrastructure.mvc.dto.RequestLog;
//import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.annotation.AfterReturning;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Before;
//import org.aspectj.lang.annotation.Pointcut;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestAttributes;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//
//@Component
//@Aspect
//public class EdiLogAspect {
//
//    private static final Logger log = LoggerFactory.getLogger(EdiLogAspect.class);
//
//    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
//    public void logPointCut() {
//        //所用api切面,用于记录request和response日志
//    }
//
//    @Before("logPointCut()")
//    public void beforeMethod(JoinPoint joinPoint) {
//        if (joinPoint.getTarget().getClass().getName().startsWith("com.up.vms")) {
////            RequestLog requestLog = handleRequest(joinPoint);
//
////            log.info(InfoDesensitizeUtil.fromObject(requestLog));
//            log.info("service begin");
//        }
//    }
//
//    @AfterReturning(returning = "obj", pointcut = "logPointCut()")
//    public void afterMethod(JoinPoint joinPoint, Object obj) {
//        if (joinPoint.getTarget().getClass().getName().startsWith("com.up.vms")) {
////            RequestLog requestLog = handleRequest(joinPoint);
////            if (obj != null && obj instanceof BaseResponse) {
////                ResponseLog responseLog = new ResponseLog();
////                responseLog.setServiceResponse((BaseResponse) obj);
////
////                responseLog.setServiceRequest(requestLog.getServiceRequest());
////                responseLog.setClassName(requestLog.getClassName());
////                responseLog.setMethodName(requestLog.getMethodName());
////                responseLog.setCostTime(System.currentTimeMillis() - requestLog.getRequestTime());
////
////                log.info(InfoDesensitizeUtil.fromObject(responseLog));
////            }
//            log.info("service finished");
//        }
//    }
//
//    private RequestLog handleRequest(JoinPoint joinPoint) {
//        String requestURL;
//        EdiHttpHeader header = null;
//        BaseRequest request = null;
//        RequestLog requestLog;
//
//        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
//        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
//        HttpServletRequest httpServletRequest = sra.getRequest();
//        String method = httpServletRequest.getMethod();
//
//        requestURL = httpServletRequest.getRequestURL().toString();
//
//        if ("GET".equals(method) || "PUT".equals(method)) {
//            String queryString = httpServletRequest.getQueryString();
//
//            if (StringUtils.isNotEmpty(queryString)) {
//                requestURL = String.format("%s?%s", requestURL, queryString);
//            }
//
//            header = new EdiHttpHeader();
//            header.setClientKey(httpServletRequest.getHeader(EdiHeaders.X_EDI_CLIENT_KEY));
//            header.setRequestId(httpServletRequest.getHeader(EdiHeaders.X_EDI_REQUEST_ID));
//            header.setRequestTime(httpServletRequest.getHeader(EdiHeaders.X_EDI_SIGNATURE));
//            header.setUserId(httpServletRequest.getHeader(EdiHeaders.X_EDI_USER_ID));
//            header.setAccessToken(httpServletRequest.getHeader(EdiHeaders.X_EDI_USER_TOKEN));
//            header.setSignature(httpServletRequest.getHeader(EdiHeaders.X_EDI_SIGNATURE));
//
//            request = new BaseRequest();
//            request.setEdiHttpHeader(header);
//        }
//
//        Object[] methodArgs = joinPoint.getArgs();
//        if (methodArgs != null) {
//            for (int i = 0; i < methodArgs.length; i++) {
//                if (methodArgs[i] instanceof BaseRequest) {
//                    request = (BaseRequest) methodArgs[i];
//                }
//                if (methodArgs[i] instanceof HttpServletRequest) {
//                    header = new EdiHttpHeader();
//                    HttpServletRequest httpRequest = (HttpServletRequest) methodArgs[i];
//                    header.setClientKey(httpRequest.getHeader(EdiHeaders.X_EDI_CLIENT_KEY));
//                    header.setRequestId(httpRequest.getHeader(EdiHeaders.X_EDI_REQUEST_ID));
//                    header.setRequestTime(httpRequest.getHeader(EdiHeaders.X_EDI_SIGNATURE));
//                    header.setUserId(httpRequest.getHeader(EdiHeaders.X_EDI_USER_ID));
//                    header.setAccessToken(httpRequest.getHeader(EdiHeaders.X_EDI_USER_TOKEN));
//                    header.setSignature(httpRequest.getHeader(EdiHeaders.X_EDI_SIGNATURE));
//                }
//            }
//        }
//
//        if (request == null) {
//            request = new BaseRequest();
//        }
//
//        if (header != null) {
//            request.setEdiHttpHeader(header);
//        } else {
//            request.setEdiHttpHeader(new EdiHttpHeader());
//        }
//
//        requestLog = new RequestLog();
//        requestLog.setRequestURL(requestURL);
//        requestLog.setServiceRequest(request);
//        requestLog.setClassName(joinPoint.getTarget().getClass().getName());
//        requestLog.setMethodName(joinPoint.getSignature().getName());
//        requestLog.setRequestTime(System.currentTimeMillis());
//
//        return requestLog;
//    }
//}
